# -*- coding: utf-8 -*-
from lxml import etree
from lxml import builder

class Scenes:

    def __init__(self):
        self.xml = builder.ElementMaker()
        self.scenes = self.xml.scenes()

    def add_scene(self, scene):
        self.scenes.append(scene)

    def output(self):
        return etree.tostring(self.scenes, encoding="utf-8", pretty_print=True)

class Scene:
    
    def __init__(self, name=""):
        self.xml = builder.ElementMaker()
        self.actions = self.xml.scene(name=name)

    def add_bg(self, src, effect=None, time=0):
        """
        添加背景图片
        :param src: 图片地址
        :param effect: 特效代码(可选)
        :param time: 特效持续时间(若有特效则为必选)
        :return:
        """
        kargs = {"src": src}
        if effect:
            kargs["effect"] = str(effect)
            kargs["time"] = str(time)
        self.actions.append(self.xml.bg(**kargs))

    def add_voice(self, src, track):
        """
        添加语音
        :param src: 音频地址
        "param track: 声道
        :return:
        """
        self.actions.append(self.xml.audio(src=src, track=track))

    def add_bgm(self, src):
        """
        添加背景音乐
        :params src: 音频地址
        :return:
        """
        self.actions.append(self.xml.bgm(src=src))

    def add_ld(self, position, src, effect=None, time=0):
        """
        添加人物立绘
        :param position: 立绘位置 ["l": 左, "c": 中, "r": 右]
        :param src: 立绘地址
        :param effect: 特效代码(可选)
        :param time: 特效持续时间(若有特效则为必选)
        :return:
        """
        kargs = {
            "position": position,
            "src": src
        }
        if effect:
            kargs["effect"] = effect
            kargs["time"] = time
        self.actions.append(self.xml.ld(**kargs))

    def add_text(self, text, mode="next"):
        """
        添加文字显示
        :param text: 文字内容
        :param mode: 下一行的显示模式 ["next": 抛弃当前内容,显示下行文字, "continue": 保留当前内容, 换行显示]
        :return:
        """
        self.actions.append(self.xml.text(text, mode=mode))

    def add_other(self, nodename, kargs):
        """
        添加其他命令
        :param nodename: 命令名称
        :param kargs: 参数dict
        :return:
        """
        eval("self.actions.append(self.xml."+nodename+"(**kargs))")

    def output(self):
        return self.actions
